#include "MountainRecordModel.h"
#include <QDebug>
#include <QStandardPaths>
#include <QDir>

MountainRecordModel::MountainRecordModel(QObject *parent)
    : QAbstractListModel(parent)
    , m_nextId(1)
{
    initializeDefaultRecords();
}

void MountainRecordModel::initializeDefaultRecords()
{
    // 添加一些示例登山记录
    addRecord("泰山", "山东省泰安市", 36.2677, 117.1006, 
              QStringList(), "五岳之首，登顶成功！", 1545.0, 8.5, 480, "晴朗", 3);
    
    addRecord("黄山", "安徽省黄山市", 30.1394, 118.1675, 
              QStringList(), "奇松怪石，云海美景", 1864.8, 12.0, 600, "多云", 4);
    
    addRecord("华山", "陕西省渭南市", 34.4889, 110.0919, 
              QStringList(), "险峻华山，挑战成功", 2154.9, 15.0, 720, "晴朗", 5);
    
    addRecord("峨眉山", "四川省乐山市", 29.5518, 103.3349, 
              QStringList(), "佛教名山，金顶日出", 3099.0, 20.0, 900, "小雨", 4);
}

int MountainRecordModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return m_records.size();
}

QVariant MountainRecordModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() < 0 || index.row() >= m_records.size())
        return QVariant();

    const MountainRecord &record = m_records.at(index.row());
    switch (role) {
    case IdRole: return record.id;
    case MountainNameRole: return record.mountainName;
    case LocationRole: return record.location;
    case LatitudeRole: return record.latitude;
    case LongitudeRole: return record.longitude;
    case DateRole: return record.date;
    case PhotosRole: return QVariant::fromValue(record.photos);
    case DescriptionRole: return record.description;
    case AltitudeRole: return record.altitude;
    case DistanceRole: return record.distance;
    case DurationRole: return record.duration;
    case WeatherRole: return record.weather;
    case DifficultyRole: return record.difficulty;
    default: return QVariant();
    }
}

QHash<int, QByteArray> MountainRecordModel::roleNames() const
{
    return {
        {IdRole, "id"},
        {MountainNameRole, "mountainName"},
        {LocationRole, "location"},
        {LatitudeRole, "latitude"},
        {LongitudeRole, "longitude"},
        {DateRole, "date"},
        {PhotosRole, "photos"},
        {DescriptionRole, "description"},
        {AltitudeRole, "altitude"},
        {DistanceRole, "distance"},
        {DurationRole, "duration"},
        {WeatherRole, "weather"},
        {DifficultyRole, "difficulty"}
    };
}

void MountainRecordModel::addRecord(const QString &mountainName, const QString &location,
                                   double latitude, double longitude, const QStringList &photos,
                                   const QString &description, float altitude, float distance,
                                   int duration, const QString &weather, int difficulty)
{
    beginInsertRows(QModelIndex(), m_records.size(), m_records.size());
    
    MountainRecord record;
    record.id = m_nextId++;
    record.mountainName = mountainName;
    record.location = location;
    record.latitude = latitude;
    record.longitude = longitude;
    record.date = QDateTime::currentDateTime();
    record.photos = photos;
    record.description = description;
    record.altitude = altitude;
    record.distance = distance;
    record.duration = duration;
    record.weather = weather;
    record.difficulty = difficulty;
    
    m_records.append(record);
    endInsertRows();
    
    updateStatistics();
}

void MountainRecordModel::removeRecord(int id)
{
    for (int i = 0; i < m_records.size(); ++i) {
        if (m_records[i].id == id) {
            beginRemoveRows(QModelIndex(), i, i);
            m_records.removeAt(i);
            endRemoveRows();
            updateStatistics();
            return;
        }
    }
}

void MountainRecordModel::updateRecord(int id, const QString &mountainName, const QString &location,
                                      double latitude, double longitude, const QStringList &photos,
                                      const QString &description, float altitude, float distance,
                                      int duration, const QString &weather, int difficulty)
{
    for (int i = 0; i < m_records.size(); ++i) {
        if (m_records[i].id == id) {
            m_records[i].mountainName = mountainName;
            m_records[i].location = location;
            m_records[i].latitude = latitude;
            m_records[i].longitude = longitude;
            m_records[i].photos = photos;
            m_records[i].description = description;
            m_records[i].altitude = altitude;
            m_records[i].distance = distance;
            m_records[i].duration = duration;
            m_records[i].weather = weather;
            m_records[i].difficulty = difficulty;
            
            QModelIndex modelIndex = index(i, 0);
            emit dataChanged(modelIndex, modelIndex);
            updateStatistics();
            return;
        }
    }
}

QVariantList MountainRecordModel::getAllRecords() const
{
    QVariantList list;
    for (const MountainRecord &record : m_records) {
        QVariantMap map;
        map["id"] = record.id;
        map["mountainName"] = record.mountainName;
        map["location"] = record.location;
        map["latitude"] = record.latitude;
        map["longitude"] = record.longitude;
        map["date"] = record.date;
        map["photos"] = QVariant::fromValue(record.photos);
        map["description"] = record.description;
        map["altitude"] = record.altitude;
        map["distance"] = record.distance;
        map["duration"] = record.duration;
        map["weather"] = record.weather;
        map["difficulty"] = record.difficulty;
        list.append(map);
    }
    return list;
}

QVariantMap MountainRecordModel::getRecordById(int id) const
{
    for (const MountainRecord &record : m_records) {
        if (record.id == id) {
            QVariantMap map;
            map["id"] = record.id;
            map["mountainName"] = record.mountainName;
            map["location"] = record.location;
            map["latitude"] = record.latitude;
            map["longitude"] = record.longitude;
            map["date"] = record.date;
            map["photos"] = QVariant::fromValue(record.photos);
            map["description"] = record.description;
            map["altitude"] = record.altitude;
            map["distance"] = record.distance;
            map["duration"] = record.duration;
            map["weather"] = record.weather;
            map["difficulty"] = record.difficulty;
            return map;
        }
    }
    return QVariantMap();
}

int MountainRecordModel::totalRecords() const
{
    return m_records.size();
}

float MountainRecordModel::totalDistance() const
{
    float total = 0.0f;
    for (const MountainRecord &record : m_records) {
        total += record.distance;
    }
    return total;
}

int MountainRecordModel::totalDuration() const
{
    int total = 0;
    for (const MountainRecord &record : m_records) {
        total += record.duration;
    }
    return total;
}

void MountainRecordModel::updateStatistics()
{
    emit totalRecordsChanged();
    emit totalDistanceChanged();
    emit totalDurationChanged();
}
